import {defineStore} from 'pinia'
import cache from '@/utils/cache'
import type {RouteRecordRaw} from 'vue-router'
import {ECacheKey} from '@/config/enums'
import RouterManager from "@/manager/base/RouterManager";
import {PageEnum} from "@/config/enums/pageEnum";
import router from "@/base/router";
import {getSystemAdminInfo} from "@/api/system/SystemAuthAdminController";
import {getSystemMenuRoute} from "@/api/system/SystemAuthMenuController";
import {login, logout} from "@/api/system/SystemLoginController";


export interface IState {
    token: string
    userInfo: Record<string, any>
    routes: RouteRecordRaw[]
    menu: any[]
    perms: string[]
}

const useUserStore = defineStore('user', {
    state: (): IState => ({
        token: cache.get(ECacheKey.TOKEN) || '',
        // 用户信息
        userInfo: {},
        // 路由
        routes: [],
        // 枚举
        menu: [],
        // 权限
        perms: []
    }),


    actions: {
        async resetLoginInfo() {
            this.token = ''
            this.userInfo = {}
            this.perms = []
            cache.remove(ECacheKey.TOKEN)
            RouterManager.resetRouter();
        },

        /**
         * 登陆
         * @param payload
         */
        login(payload: any) {
            const {account, password} = payload
            return new Promise((resolve, reject) => {
                login({
                    username: account.trim(),
                    password: password
                })
                    .then((data) => {
                        this.token = data.token
                        cache.set(ECacheKey.TOKEN, data.token)
                        resolve(data)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        },

        /**
         * 推出登陆
         */
        logout: function () {
            return new Promise(async (resolve, reject) => {
                logout()
                    .then(async (data) => {
                        await this.resetLoginInfo()
                        await router.push(PageEnum.LOGIN)
                        resolve(data)
                    })
                    .catch((error) => {
                        reject(error)
                    })


            })
        },

        /**
         * 获取用户信息
         */
        getSystemAdminInfo() {
            return new Promise((resolve, reject) => {
                getSystemAdminInfo()
                    .then((data) => {
                        // 用户数据
                        this.userInfo = data
                        // 权限
                        this.perms = data.permissions

                        resolve(data)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        },


        /**
         * 获取路由枚举
         *
         */
        getSystemMenuRoute() {
            return new Promise((resolve, reject) => {
                getSystemMenuRoute()
                    .then((data) => {
                        this.menu = data
                        this.routes = RouterManager.filterAsyncRoutes(data)


                        resolve(data)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        }
    }
})

export default useUserStore
